package com.geovis.emergency.spd.controller.bi;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.geovis.common.core.api.Result;
import com.geovis.common.core.constant.SwaggerConstants;
import com.geovis.emergency.spd.biz.pc.service.*;
import com.geovis.emergency.spd.cache.DictCache;
import com.geovis.emergency.spd.entity.pc.entity.PcDrillDutyOrg;
import com.geovis.emergency.spd.entity.pc.pojo.constant.PcDisasterConstant;
import com.geovis.emergency.spd.entity.pc.pojo.dto.PcDrillDisasterEventQueryDTO;
import com.geovis.emergency.spd.entity.pc.pojo.enums.EventStageEnum;
import com.geovis.emergency.spd.entity.pc.pojo.vo.BIDrillDisasterEventVO;
import com.geovis.emergency.spd.entity.pc.pojo.vo.PcDrillDisasterEventVO;
import com.geovis.emergency.spd.entity.pc.pojo.vo.PcDrillPlanVO;
import com.geovis.emergency.spd.entity.pc.pojo.vo.PcDrillVO;
import com.geovis.emergency.spd.entity.system.entity.SystemDictData;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ：linwl
 * @date ：Created in 2022/9/26 10:04
 * @description：大屏的事件相关接口
 * @version: v1
 */
@RestController
@RequestMapping("/bi/pc/drill/disaster/event")
@RequiredArgsConstructor
@Api(value = "大屏-相关接口", tags = "大屏-相关接口")
@Slf4j
@Validated
public class BiInterface {

    private final IPcDrillDisasterEventService pcDrillDisasterEventService;
    private final IPcDrillDisasterService pcDrillDisasterService;
    private final IPcDrillService pcDrillService;
    private final IPcDrillDutyOrgService pcDrillDutyOrgService;
    private final IPcDrillPlanService pcDrillPlanService;

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "根据主键ID查询演练详情", notes = "根据主键ID查询演练详情")
    @PostMapping("/getById/{drillId}")
    public Result<PcDrillVO> getById(@PathVariable("drillId") String drillId) {
        PcDrillVO pcDrillVO = pcDrillService.detailPcDrill(drillId);
        List<String> groupList = pcDrillDutyOrgService.myDutyGroupList(drillId, false);
        if (ObjectUtil.isNotEmpty(groupList)) {
            PcDrillDutyOrg drillDutyOrg = pcDrillDutyOrgService.getById(groupList.get(0));
            if (ObjectUtil.isNotNull(drillDutyOrg)) {
                pcDrillVO.setDutyOrgName(drillDutyOrg.getOrgName());
            }
        }
        return Result.ok(pcDrillVO);
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "获取事件列表", notes = "获取事件列表")
    @GetMapping("eventList/{pcDrillId}")
    public Result<List<BIDrillDisasterEventVO>> eventList(@PathVariable("pcDrillId") @ApiParam("演练id") String pcDrillId) {
        List<BIDrillDisasterEventVO> eventVOList = Lists.newArrayList();
        List<SystemDictData> dictDataList = DictCache.getDictData(PcDisasterConstant.EVENT_STAGE);
        for (SystemDictData systemDictData : dictDataList) {
            List<String> idList = pcDrillDisasterEventService.listIdByDrillIdAndEventStage(pcDrillId, systemDictData.getDataKey());
            if (CollUtil.isNotEmpty(idList)){
                BIDrillDisasterEventVO vo = new BIDrillDisasterEventVO();
                vo.setGroupName(systemDictData.getDataValue());
                vo.setDisaster(idList);
                eventVOList.add(vo);
            }
        }
        return Result.ok(eventVOList);
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "获取灾害事件或者演练灾害事件", notes = "获取灾害事件详情")
    @PostMapping({"/getDetail"})
    public Result<PcDrillDisasterEventVO> getEventDetail(@RequestBody PcDrillDisasterEventQueryDTO pcDrillDisasterEventQueryDTO) {
        return Result.ok(pcDrillDisasterService.getDetail(pcDrillDisasterEventQueryDTO));
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "方案集合", notes = SwaggerConstants.LIST_NOTES)
    @PostMapping({"/getList/{drillId}/{drillDisasterEventId}"})
    public Result<List<PcDrillPlanVO>> getList(
            @PathVariable("drillId") String drillId,
            @PathVariable("drillDisasterEventId") String drillDisasterEventId,
            @RequestParam(required = false) String groupId,
            @RequestParam(required = false) String status) {
        if (StrUtil.isBlank(groupId)) {
            List<String> groupList = pcDrillDutyOrgService.myDutyGroupList(drillId, false);
            if (groupList.size() > 0) {
                groupId = groupList.get(0);
            }
        }
        return Result.ok(pcDrillPlanService.pcDrillPlanList(drillId, drillDisasterEventId, groupId, status));
    }
}
